package com.gs.synedocket.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.appcompat.widget.SwitchCompat;

import com.gs.synedocket.R;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.constant.EventCode;
import com.gs.synedocket.db.bean.Event;
import com.gs.synedocket.db.dao.LogUtil;
import com.gs.synedocket.fragment.base.BaseFragment;
import com.gs.synedocket.presenter.SettingPresenter;
import com.gs.synedocket.util.LanguageUtils;
import com.gs.synedocket.util.SPUtil;
import com.gs.synedocket.util.ToastUtil;
import com.gs.synedocket.view.ISettingView;

import org.greenrobot.eventbus.EventBus;

import java.util.Locale;

public class SettingFragment extends BaseFragment<ISettingView, SettingPresenter> implements ISettingView {

    private SwitchCompat shEnglish, shChinese, shDownload, shAuto;
    private EditText etUploadTime, etDownloadTime;
    private LinearLayout llSyncTime;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_setting;
    }

    @Override
    protected SettingPresenter createPresenter() {
        return new SettingPresenter();
    }

    @Override
    protected void initView() {
        shChinese = fd(R.id.sh_chinese);
        shEnglish = fd(R.id.sh_english);
        etUploadTime = fd(R.id.et_upload_time);
        etDownloadTime = fd(R.id.et_download_time);
        shDownload = fd(R.id.sh_download);
        shAuto = fd(R.id.sh_auto);
        llSyncTime = fd(R.id.ll_sync_time);
        MyOnCheckChangeListener onCheckChangeListener = new MyOnCheckChangeListener();
        shChinese.setOnCheckedChangeListener(onCheckChangeListener);
        shEnglish.setOnCheckedChangeListener(onCheckChangeListener);
        shAuto.setOnCheckedChangeListener(onCheckChangeListener);
        fd(R.id.btn_confirm).setOnClickListener(new MyOnClickListener());
    }

    @Override
    protected void initData() {
        int old = SPUtil.getInstance().getInt(AllConstants.LANGUAGE, 1);
        if (old == 1) {
            shChinese.setChecked(true);
        } else {
            shEnglish.setChecked(true);
        }
        int time = SPUtil.getInstance().getInt(AllConstants.UPLOAD_DOCKET_INTERVAL, getResources().getInteger(R.integer.upload_interval));
        etUploadTime.setText(String.valueOf(time));
        int download = SPUtil.getInstance().getInt(AllConstants.SYNC_DOCKET_INTERVAL, getResources().getInteger(R.integer.download_interval));
        etDownloadTime.setText(String.valueOf(download));
        shDownload.setChecked(SPUtil.getInstance().getBoolean(AllConstants.IS_DOWNLOAD, true));
        shAuto.setChecked(SPUtil.getInstance().getBoolean(AllConstants.IS_AUTO, true));
        llSyncTime.setVisibility(shAuto.isChecked() ? View.VISIBLE : View.GONE);
    }

    @Override
    public int getUploadInterval() {
        String string = etUploadTime.getText().toString();
        int second = 0;
        if (!TextUtils.isEmpty(string)) {
            second = Integer.parseInt(string);
        }
        return second;
    }

    @Override
    public int getDownloadInterval() {
        String string = etDownloadTime.getText().toString();
        int second = 0;
        if (!TextUtils.isEmpty(string)) {
            second = Integer.parseInt(string);
        }
        return second;
    }

    class MyOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            if (!shChinese.isChecked() && !shEnglish.isChecked()) {
                ToastUtil.showToast(R.string.p_sel_lang);
                return;
            }
            int uploadInterval = getUploadInterval();
            int downloadInterval = getDownloadInterval();
            if (uploadInterval == 0 || downloadInterval == 0) {
                ToastUtil.showToast(R.string.p_input_sync_time);
                return;
            }
            if (uploadInterval < 3 || downloadInterval < 3) {
                ToastUtil.showToast(R.string.sync_time_limit);
                return;
            }
            changeIsAuto();
            changeSyncTime(uploadInterval, downloadInterval);
            changeLanguage();
            changeIsDownload();
            ToastUtil.showToast(R.string.update_success);
        }
    }

    private void changeIsAuto() {
        boolean old = SPUtil.getInstance().getBoolean(AllConstants.IS_AUTO, true);
        boolean newSet = shAuto.isChecked();
        if (old == newSet) {
            return;
        }
        LogUtil.getInstance().insertOrReplaceData("設置“自動同步”為" + newSet);
        SPUtil.getInstance().putBoolean(AllConstants.IS_AUTO, newSet);
    }

    private void changeIsDownload() {
        boolean old = SPUtil.getInstance().getBoolean(AllConstants.IS_DOWNLOAD, true);
        boolean newSet = shDownload.isChecked();
        if (old == newSet) {
            return;
        }
        LogUtil.getInstance().insertOrReplaceData("設置“用戶執行送貨單中，不需自動下載送貨單”為" + newSet);
        SPUtil.getInstance().putBoolean(AllConstants.IS_DOWNLOAD, newSet);
    }

    private void changeLanguage() {
        if (shChinese.isChecked()) {
            changeLanguage(1, Locale.TAIWAN, "切換中文語言");
        } else if (shEnglish.isChecked()) {
            changeLanguage(2, Locale.ENGLISH, "切換英文語言");
        }
    }

    private void changeSyncTime(int uploadInterval, int downloadInterval) {
        if (SPUtil.getInstance().getInt(AllConstants.UPLOAD_DOCKET_INTERVAL,  getResources().getInteger(R.integer.upload_interval)) != uploadInterval) {
            LogUtil.getInstance().insertOrReplaceData("修改同步上傳時間為" + uploadInterval + "s");
            SPUtil.getInstance().putInt(AllConstants.UPLOAD_DOCKET_INTERVAL, uploadInterval);
        }
        if (SPUtil.getInstance().getInt(AllConstants.SYNC_DOCKET_INTERVAL, getResources().getInteger(R.integer.download_interval)) != downloadInterval) {
            LogUtil.getInstance().insertOrReplaceData("修改同步下載時間為" + downloadInterval + "s");
            SPUtil.getInstance().putInt(AllConstants.SYNC_DOCKET_INTERVAL, downloadInterval);
        }
    }

    private void changeLanguage(int languageId, Locale locale, String msg) {
        if (SPUtil.getInstance().getInt(AllConstants.LANGUAGE, 1) == languageId) {
            return;
        }
        LogUtil.getInstance().insertOrReplaceData(msg);
        SPUtil.getInstance().putInt(AllConstants.LANGUAGE, languageId);
        LanguageUtils.getInstance().change(locale);
        EventBus.getDefault().post(new Event(EventCode.EVENT_LANGUAGE));
    }

    class MyOnCheckChangeListener implements CompoundButton.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (buttonView.getId() == R.id.sh_auto) {
                llSyncTime.setVisibility(isChecked ? View.VISIBLE : View.GONE);
                return;
            }
            int id = buttonView.getId();
            if (id == R.id.sh_chinese) {
                shEnglish.setChecked(!isChecked);
            } else if (id == R.id.sh_english) {
                shChinese.setChecked(!isChecked);
            }
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            initData();
        }
    }
}
